package com.youdianda.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youdianda.constant.MessageConstant;
import com.youdianda.dto.YddAdvposDTO;
import com.youdianda.dto.YddAdvposPageQueryDTO;
import com.youdianda.entity.YddAdvimg;
import com.youdianda.entity.YddAdvpos;
import com.youdianda.mapper.YddAdvimgMapper;
import com.youdianda.mapper.YddAdvposMapper;
import com.youdianda.result.Result;
import com.youdianda.service.IYddAdvposService;
import com.youdianda.vo.YddAdvposPageQueryVO;
import com.youdianda.vo.YddAdvposVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mosen
 * @since 2023-05-15
 */
@Service
public class YddAdvposServiceImpl extends ServiceImpl<YddAdvposMapper, YddAdvpos> implements IYddAdvposService {

    @Autowired
    private YddAdvposMapper yddAdvposMapper;

    @Autowired
    private YddAdvimgMapper yddAdvimgMapper;


    /**
     * 广告位分页查询
     *
     * @param yddAdvposPageQueryDTO
     * @return
     */
    @Override
    public Result<YddAdvposPageQueryVO> pageQuery(YddAdvposPageQueryDTO yddAdvposPageQueryDTO) {
        //1.非空判断
        if (yddAdvposPageQueryDTO == null) {
            return Result.error(MessageConstant.ORDER_STATUS_ERROR);
        }

        //2.业务判断
        String advposdesc = yddAdvposPageQueryDTO.getAdvposdesc();
        String advposname = yddAdvposPageQueryDTO.getAdvposname();

        LambdaQueryWrapper<YddAdvpos> lqw = new LambdaQueryWrapper<YddAdvpos>();

        if (advposdesc != null) {
            lqw.like(YddAdvpos::getAdvposdesc, advposdesc);
        }

        if (advposname != null) {
            lqw.like(YddAdvpos::getAdvposname, advposname);
        }

        //3.使用分页查询
        // 3.1创建Ipage分页对象，设置分页参数
        IPage<YddAdvpos> page = new Page<>(yddAdvposPageQueryDTO.getPage(), yddAdvposPageQueryDTO.getLimit());

        //3.2执行分页查询
        yddAdvposMapper.selectPage(page, lqw);

        //3.3封装数据
        YddAdvposPageQueryVO vo = YddAdvposPageQueryVO.builder()
                .currentPage(page.getCurrent())
                .pageSize(page.getSize())
                .totalPages(page.getPages())
                .data(page.getRecords())
                .count(page.getTotal())
                .build();

       //4.响应数据
        return Result.success(vo);
    }

    /**
     * 根据id查询广告位详情
     *
     * @param id
     * @return
     */
    @Override
    public Result<YddAdvposVO> selectById(Integer id) {
        //1.非空判断
        if(id == null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //2.业务判断
        YddAdvpos yddAdvpos = yddAdvposMapper.selectById(id);
        YddAdvposVO vo = new YddAdvposVO();
        BeanUtils.copyProperties(yddAdvpos,vo);

        //3.响应数据
        return Result.success(vo);
    }

    /**
     * 修改广告位
     *
     * @param yddAdvposDTO
     * @return
     */
    @Override
    public Result updateYddAdvpos(YddAdvposDTO yddAdvposDTO) {
        //1.非空判断
        if(yddAdvposDTO == null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        //2. 修改数据
        YddAdvpos ydd= new YddAdvpos();
        BeanUtils.copyProperties(yddAdvposDTO,ydd);
        yddAdvposMapper.updateById(ydd);

        //3.响应数据
        return Result.success();
    }

    /**
     * 根据id删除广告位
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result deleteById(Integer id) {
        //1.非空判断
        if(id == null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }

        //2. 业务判断
        //2.1根据id查找关联表广告位

        LambdaQueryWrapper<YddAdvimg> lqw = new LambdaQueryWrapper<YddAdvimg>();
        lqw.eq(YddAdvimg::getAdvimgpos,id);
        yddAdvimgMapper.delete(lqw);

        yddAdvposMapper.deleteById(id);

        //3.响应数据
        return Result.success();
    }

    /**
     * 广告列表全部
     *
     * @param
     * @return
     */
    @Override
    public Result<List<YddAdvpos>> allAdvpos() {

        //1.业务判断
        List<YddAdvpos> yddAdvpos = yddAdvposMapper.selectList(null);

        //2.响应数据
        return Result.success(yddAdvpos);
    }

    /**
     * 新增广告位
     *
     * @param yddAdvposDTO
     * @return
     */
    @Override
    public Result insertAdvpos(YddAdvposDTO yddAdvposDTO) {
        //1.非空判断
        if(yddAdvposDTO == null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }



        //2.业务判断
        YddAdvpos yddAdvpos = new YddAdvpos();
        BeanUtils.copyProperties(yddAdvposDTO,yddAdvpos);
        yddAdvpos.setDefaultData("1");
        yddAdvposMapper.insert(yddAdvpos);

        //3.响应数据
        return Result.success();
    }


}
